package cn.edu.zhku.citrusmonitor.util;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.edu.zhku.citrusmonitor.constant.Constant;

public class PrintMessageUtil {
	// 根据情况输出结果
	public static String printMessage(HttpServletResponse response, boolean res) throws IOException {
		if(res) {
			return JSON.toJSONString(Message.ok());
		}else {
			return JSON.toJSONString(Message.errMsg("信息不存在!"));
		}
	}
	// 根据情况输出结果
	public static String printMessage(HttpServletResponse response, Object obj) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		if(obj != null) {
			return JSON.toJSONString(Message.ok(obj));
		}else {
			return JSON.toJSONString(Message.errMsg("信息不存在!"));
		}
	}
	
	// 根据情况输出结果
	public static String printMessageWithDateFormat(HttpServletResponse response, Object obj, String format) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		if(format == null) {
			format = Constant.DEFAULT_DATEFORMAT;
		}
		if(obj != null) {
			return JSON.toJSONStringWithDateFormat(Message.ok(obj), format, SerializerFeature.EMPTY);
		}else {
			return JSON.toJSONString(Message.errMsg("信息不存在!"));
		}
	}
	
	public static String printListMessageWithDateFormat(HttpServletResponse response, List<?> list, String format) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		if(format == null) {
			format = Constant.DEFAULT_DATEFORMAT;
		}
		if(list != null && list.size() != 0) {
			return JSON.toJSONStringWithDateFormat(Message.ok(list), format, SerializerFeature.EMPTY);
		}else {
			return JSON.toJSONString(Message.errMsg("信息不存在!"));
		}
	}
	
	public static String printListMessage(HttpServletResponse response, List<?> list) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		if(list != null && list.size() != 0) {
			return JSON.toJSONString(Message.ok(list));
		}else {
			return JSON.toJSONString(Message.errMsg("信息不存在!"));
		}
	}
}
